﻿package forms
{
	import AliveChessLibrary.Commands.RegisterCommands.AuthorizeRequest;
	import AliveChessLibrary.Commands.RegisterCommands.AuthorizeResponse;
	
	import Net.ProtoBufferCodec;
	import Net.Transport;
	
	import Symbols.connectionLog;
	
	import com.google.protobuf.Message;
	
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.ComboBox;
	import fl.controls.Label;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import fl.events.ComponentEvent;
	
	import flash.display.*;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	
	import main.Main;
	
	//по сути этот класс уже не нужен, но пока пусть побудет

	public class formAuthorize extends MovieClip
	{
		//private var con:Connect;
		private var txtLogin:TextInput;
		private var txtPas:TextInput;
		private var cb:CheckBox;
		private var lAuthorize:Label;
		private var lLogin:Label;
		private var lPas:Label;
		private var lType:Label;
		private var btn:Button;
		
		private var tf:TextFormat=new TextFormat();
		var lPlayer:Label = new Label();
		private var type:Boolean = true;
		private var m:Main;
		private var transport:Transport;
		private var ar:AuthorizeResponse;
		
		public function formAuthorize(m:Main)
		{
			this.m=m;
			transport = new Transport(this.m,null);
			transport.addEventListener(Transport.RESPONSE_COMPLITE,responseExecute);
			/*var boxShadow:Sprite = new Sprite();
			boxShadow.graphics.lineStyle(1);
			boxShadow.graphics.beginFill(0xFFFFFF/*0xFF3300);
			boxShadow.alpha=0,7;
			boxShadow.graphics.drawRect(160,90,230,204);
			boxShadow.graphics.endFill();
			addChild(boxShadow);*/
			var t:title1=new title1();
			var r:rect=new rect();
			t.x = 5;
			r.x = 160;
			t.y = 10;
			r.y = 90;
			addChild(t);
			addChild(r);
			createUI();
			setUpHandlers();
			//соединение с сервером
		
		}
		private function responseExecute(e:Event):void
		{
			ar = AuthorizeResponse (transport.msgResponse);
			trace (ar.IsAuthorized.toString()+" "+ar.IsNewPlayer.toString()+ar.ErrorMessage);
		}
		private function createUI():void
		{
			tf.size = 18;
			tf.font = "Times New Roman";
			bldLabels();
			bldTxtInput();
			bldCB();
			bldBtn();
		}
		private function bldLabels():void
		{
			lAuthorize=new Label();
			lAuthorize.setSize(100,30);
			lAuthorize.text = "Authorize";
			lAuthorize.move(165,100);
			lAuthorize.setStyle("textFormat",tf);
			addChild(lAuthorize);
			lLogin=new Label();
			lLogin.setSize(100,30);
			lLogin.text = "Login";
			lLogin.move(165,135);
			lLogin.setStyle("textFormat",tf);
			addChild(lLogin);
			lPas=new Label();
			lPas.setSize(100,30);
			lPas.text = "Password";
			lPas.move(165,170);
			lPas.setStyle("textFormat",tf);
			addChild(lPas);
			lType=new Label();
			lType.setSize(100,30);
			lType.text = "Type";
			lType.move(165,210);
			lType.setStyle("textFormat",tf);
			addChild(lType);
		}
		private function bldTxtInput():void
		{
			txtLogin=new TextInput();
			txtLogin.setSize(100,20);
			txtLogin.move(275,135);
			txtLogin.text = "player";
			addChild(txtLogin);
			txtPas=new TextInput();
			txtPas.setSize(100,20);
			txtPas.move(275,170);
			txtPas.text = "pw";
			txtPas.displayAsPassword = true;
			txtPas.editable = true;
			addChild(txtPas);
		}
		private function bldCB():void
		{
			cb=new CheckBox();
			cb.setSize(100,20);
			cb.selected = true;
			cb.label = "Super User";
			cb.move(270,210);
			addChild(cb);
		}
		private function bldBtn():void
		{
			btn = new Button();
			btn.label = "Ok";
			btn.selected = true;
			btn.setSize(100,22);
			btn.move(275,245);
			addChild(btn);
		}
		private function setUpHandlers():void
		{
			btn.addEventListener(MouseEvent.CLICK,clickHandler);
			cb.addEventListener(MouseEvent.CLICK,cbHandler);
		}
		private function cbHandler(e:MouseEvent):void
		{
			type = e.target.selected;
		}
		private function clickHandler(e:MouseEvent):void
		{
			var ta:TextArea=new TextArea();
			ta.text = " ";
			ta.setSize(300,25);
			ta.move(10,345);
			ta.text +=  "Login: " + txtLogin.text + ", Password: " + txtPas.text + ", Type: " + type.toString();
			addChild(ta);
			var c:connectionLog=new connectionLog();
			var ar:AuthorizeRequest = new AuthorizeRequest();
			ar.login = txtLogin.text;
			ar.password = txtPas.text;
			transport.SendCommand(ar);
			//con = new Connect(m,txtLogin.text,txtPas.text,type/*,ta*/);
					
				
		}
		
	}

}